package cn.dawn.webcollector.model;

import java.io.UnsupportedEncodingException;

import com.sleepycat.je.DatabaseEntry;

/**
 * 
 * <p>任务数据库</p>
 * <p>Description:XXXXXXXX</p>
 * @Copyright:Copyright (c) 2015
 * @Author:wangxiao
 * @Version:1.0
 * @Time:2015年10月10日
 */
public class CrawlDatum {
	/**
	 * 爬取状态常量-注入
	 */
	public static final byte STATUS_DB_INJECTED = 0x01;
	/**
	 * 爬取状态常量-未爬取
	 */
	public static final byte STATUS_DB_UNFETCHED = 0x04;
	/**
	 * 爬取状态常量-已爬取
	 */
	public static final byte STATUS_DB_FETCHED = 0x05;

	private String url;
	private byte status = CrawlDatum.STATUS_DB_INJECTED;
	private int retry = 0;

	public DatabaseEntry getKey() throws UnsupportedEncodingException {
		return new DatabaseEntry(url.getBytes("utf-8"));
	}

	public DatabaseEntry getValue() {
		byte[] value = new byte[2];
		value[0] = status;
		value[1] = (byte) retry;
		return new DatabaseEntry(value);
	}

	public CrawlDatum(String url, byte status) {
		this.url = url;
		this.status = status;
		this.retry = 0;
	}

	public CrawlDatum(String url, byte status, int retry) {
		this.url = url;
		this.status = status;
		this.retry = retry;
	}

	public CrawlDatum(DatabaseEntry key, DatabaseEntry value) throws UnsupportedEncodingException {
		this.url = new String(key.getData(), "utf-8");
		byte[] valueData = value.getData();
		this.status = valueData[0];
		this.retry = valueData[1];
	}

	/**
	 * 获取爬取任务的url
	 * 
	 * @return 爬取任务的url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * 设置爬取任务的url
	 * 
	 * @param url
	 *            爬取任务的url
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * 获取爬取任务的状态
	 * 
	 * @return 爬取任务的状态
	 */
	public byte getStatus() {
		return status;
	}

	/**
	 * 设置爬取任务的状态
	 * 
	 * @param status
	 *            爬取任务的状态
	 */
	public void setStatus(byte status) {
		this.status = status;
	}

	public int getRetry() {
		return retry;
	}

	public void setRetry(int retry) {
		this.retry = retry;
	}

}
